package org.classg.order.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.classg.order.service.PayLogService;
import org.classg.utils.JwtUtils;
import org.classg.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-06-16
 */
@Api(description = "支付日志管理")
@RestController
@RequestMapping("/orderservice/pay-log")
@CrossOrigin
public class PayLogController {

    @Autowired
    private PayLogService payLogService;

    @ApiOperation("根据订单号生成微信支付二维码")
    @GetMapping("/queryPaymentCodeByOrderNo/{orderNo}")
    public R queryPaymentCodeByOrderNo(@ApiParam(name = "orderNo", value = "订单号", required = true) @PathVariable String orderNo) {
        Map<String, Object> map = payLogService.queryPaymentCodeByOrderNo(orderNo);
        return R.ok().data(map);
    }

    @ApiOperation("根据订单号获取支付状态")
    @GetMapping("/queryPayStatusByOrderNo/{orderNo}")
    public R queryPayStatusByOrderNo(@ApiParam(name = "orderNo", value = "订单号", required = true) @PathVariable String orderNo,
                                     @ApiParam(name = "request", value = "用户id请求对象", required = true) HttpServletRequest request) {
        if (StringUtils.isEmpty(JwtUtils.getMemberIdByJwtToken(request))) {
            return R.ok().code(28004).message("用户未登录！");
        }
        Map<String, String> map = payLogService.queryPayStatusByOrderNo(orderNo);
        if (map == null) {
            return R.error().message("支付错误！");
        }
        if (map.get("trade_state").equals("SUCCESS")) {
            // 更新订单状态（添加支付记录和更新订单状态）
            payLogService.updateOrderStatus(map);
            return R.ok().message("支付成功！");
        }
        return R.ok().code(25000).message("支付中！");
    }
}

